﻿using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;

namespace TrinityEngine
{
    public class BatchedSpriteMaterial : Material
    {
        private Texture[] _textures = new Texture[32];

        /*        private readonly static int[] _samplers = new int[32];*/

        public BatchedSpriteMaterial()
        {
            _shader = new Shader("res/shaders/Batched Sprite.shader");
            /* for (int i = 0; i < _samplers.Length; i++)
             {
                 _samplers[i] = i;
             }*/
        }

        public override void Bind()
        {
            GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
            GL.Enable(EnableCap.Blend);

            _shader.Bind();

            for (int i = 0; i < _textures.Length; i++)
            {
                _textures[i].Bind(i);
                _shader.SetUniform1i($"u_textures[{i}]", i);
            }

            /*_shader.SetUniform1i("u_textures", _samplers, _textures.Length);*/
            /* _shader.SetUniform1i("test[0]", 1);*/
        }

        public void SetUniform(Matrix4 vp)
        {
            _shader.Bind();
            _shader.SetUniformMat4("u_vp", vp);
        }

        public void SetTextures(Texture[] textures)
        {
            _textures = textures;
        }

        public override void Unbind()
        {
            _shader.Unbind();
            for (int i = 0; i < _textures.Length; i++)
            {
                _textures[i].Unbind();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {

            }

            /*for (int i = 0; i < _textures.Length; i++)
            {
                _textures[i].Dispose();
            }*/

            base.Dispose(disposing);
        }
    }
}
